package com.mrper.ui.main;

import android.annotation.SuppressLint;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.miguo.ui.R;
import com.miguo.ui.databinding.FragmentDoubleNineBinding;
import com.mrper.ext.adapter.fragment.SwitchFragmentStateAdapter;
import com.mrper.ext.annotation.ContentView;
import com.mrper.ext.uibase.BasicFragment;
import com.mrper.model.type.Double9Type;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/3/11.
 * 九块九模块
 */
@SuppressWarnings("all")
@ContentView(R.layout.fragment_double_nine)
public class FragmentDoubleNine extends BasicFragment<FragmentDoubleNineBinding> implements ViewPager.OnPageChangeListener {

    public static final String TAG = FragmentDoubleNine.class.getSimpleName();
    public static final int MAX_TAB_COUNT = 4;//最大的Tab菜单按钮数

    private int currentTabPosition = 0;//当前默认选中0,此字段用于记录当前索引位置，不可删除
    private List<FragmentDoubleNineListContent> fragments = new ArrayList<>();

    @Override
    @SuppressWarnings({"unchecked", "deprecation"})
    protected void initViews() {
        super.initViews();
        dataBinder.setOnClickEvent(this);
        dataBinder.tabDouble9.postDelayed(new Runnable() {
            @Override
            public void run() {
                initViewLayout(); //初始化
            }
        }, 100);
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("deprecation")
    private void initViewLayout() {
        List<Double9Item> tabItems = buildDouble9Items();
        int itemCount = tabItems.size();
        String[] titles = new String[itemCount];
        for(int i = 0; i < itemCount;i++){
            Double9Item item = tabItems.get(i);
            titles[i] = item.title;
            dataBinder.tabDouble9.addTab(dataBinder.tabDouble9.newTab(), i , i == 0);
            fragments.add(new FragmentDoubleNineListContent(item.type));
        }
        SwitchFragmentStateAdapter<FragmentDoubleNineListContent> fragmentAdapter
                = new SwitchFragmentStateAdapter<FragmentDoubleNineListContent>(getChildFragmentManager(), fragments, titles);
        dataBinder.vpDouble9Content.setAdapter(fragmentAdapter);
        dataBinder.tabDouble9.setupWithViewPager(dataBinder.vpDouble9Content);
        if (Build.VERSION.SDK_INT < 23) dataBinder.vpDouble9Content.setOnPageChangeListener(this);
        else dataBinder.vpDouble9Content.addOnPageChangeListener(this);
        fragments.get(0).initDataLoad(); //初始化数据加载
    }

    @NonNull
    private List<Double9Item> buildDouble9Items() {
        List<Double9Item> tabItems = new ArrayList<>();
        tabItems.add(new Double9Item(0, "9块9", Double9Type.Double9));
        tabItems.add(new Double9Item(0, "19块9", Double9Type.TenDouble9));
        tabItems.add(new Double9Item(0, "29块9", Double9Type.TwentyDouble9));
        tabItems.add(new Double9Item(0, "49块9", Double9Type.FourtyDouble9));
        return tabItems;
    }

    public void onClick(View view) {  }

    @Override
    public void onPageSelected(int position) {
        fragments.get(position).initDataLoad();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {  }


    public static class Double9Item {

        public Double9Item(){}

        public Double9Item(int index, String title, Double9Type type){
            this.index = index;
            this.title = title;
            this.type = type;
        }

        public int index;

        public String title;

        public Double9Type type;

    }


}
